package com.lanou.sfu.songforu.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;
import com.lanou.sfu.songforu.adapter.RecommendAdapter;
import com.lanou.sfu.songforu.adapter.RecommendHostSongAdapter;
import com.lanou.sfu.songforu.adapter.RecommendRadioAdapter;
import com.lanou.sfu.songforu.adapter.RecommendSongListAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.datas.RecommendHostSongsDatas;
import com.lanou.sfu.songforu.datas.RecommendPlayDatas;
import com.lanou.sfu.songforu.datas.RecommendRadioDatas;
import com.lanou.sfu.songforu.datas.RecommendSongListDatas;
import com.lanou.sfu.songforu.utils.net.HttpHelper;
import com.lanou.sfu.songforu.utils.net.NetDataCallBack;
import com.lanou.sfu.songforu.utils.utils.NoScrollGridView;
import com.lanou.sfu.songforu.utils.utils.NoScrollListView;
import com.lanou.sfu.songforu.view.MyEvent;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by quyan on 15/10/21.
 */
public class RecommendFragment extends BaseFragment implements
        NetDataCallBack, ViewPager.OnPageChangeListener,AdapterView.OnItemClickListener, View.OnClickListener {
    private ViewPager viewPager;
    private RecommendAdapter recommendAdapter;
    private HttpHelper httpHelper;
    private List<RecommendPlayDatas.PicEntity> picEntities;
    private Context context;
    private LinearLayout linearLayout;
    private int current;
    private HandlerThread handlerThread;
    private Handler handler;
    private Handler startHandle;
    private boolean flag = true;
    private TextView more,moreRadio;
    //推荐歌单
    private NoScrollGridView gridView;
    private RecommendSongListAdapter songListAdapter;
    private RecommendSongListDatas.ContentEntity datas;
    //热门歌曲推荐
    private NoScrollListView listView;
    private RecommendHostSongAdapter hostSongAdapter;
    private List<RecommendHostSongsDatas.ContentEntity> contentEntities;
    //推荐电台
    private NoScrollGridView radioGrid;
    private RecommendRadioAdapter radioAdapter;
    private List<RecommendRadioDatas.ListEntity> listEntities;
    private MainActivity activity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
        activity = (MainActivity) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recommend, null);
        //轮播图
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout);
        //推荐歌单
        gridView = (NoScrollGridView) view.findViewById(R.id.gridView);
        //热门歌曲推荐
        listView = (NoScrollListView) view.findViewById(R.id.listView);
        //电台节目
        radioGrid = (NoScrollGridView) view.findViewById(R.id.gridViewRadio);
        //更多
        more = (TextView) view.findViewById(R.id.more);
        moreRadio = (TextView) view.findViewById(R.id.moreRadio);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        //songListMore.setOnClickListener(this);
        more.setOnClickListener(this);
        moreRadio.setOnClickListener(this);

    }

    public void initView() {
        //轮播图
        startHandle = new Handler();
        httpHelper = new HttpHelper(this);
        httpHelper.getMainPage();//获取接口
        backgroudThread();
        //推荐歌单
        httpHelper.getQuestionPage();
        //热门歌曲
        httpHelper.getHostPage();
        hostSongAdapter = new RecommendHostSongAdapter(context);
        gridView.setOnItemClickListener(this);
        //电台节目
        httpHelper.getRadioPage();


    }

    @Override
    public void netError(int what) {

    }

    @Override
    public void netSuccess(int what, Object object) {
        switch (what) {
            case WHAT_MAIN_PAGE://轮播图
                picEntities = ((RecommendPlayDatas) object).getPic();//拉取数据
                recommendAdapter = new RecommendAdapter(context, picEntities);//初始化适配器
                viewPager.setAdapter(recommendAdapter);//绑定适配器
                dot();//调用圆点方法
                viewPager.setOnPageChangeListener(this);
                break;
            case WHAT_QUESTION://歌单推荐
                datas = ((RecommendSongListDatas) object).getContent();
                songListAdapter = new RecommendSongListAdapter(context, datas);
                gridView.setAdapter(songListAdapter);
                break;
            case WHAT_HOSTSONG://热门歌曲
                contentEntities = ((RecommendHostSongsDatas) object).getContent();
                hostSongAdapter.addData(contentEntities);
                listView.setAdapter(hostSongAdapter);
                break;
            case WHAT_RADIO://电台节目
                listEntities = ((RecommendRadioDatas)object).getList();
                radioAdapter = new RecommendRadioAdapter(context,listEntities);
                radioGrid.setAdapter(radioAdapter);
        }
    }

    public void dot() {
        //设置小圆点
        for (int i = 0; i < picEntities.size(); i++) {
            ImageView img = new ImageView(context);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(15, 15);
            img.setLayoutParams(lp);
            if (i == 0) {
                img.setImageResource(R.drawable.dot_focused);
            } else {
                img.setImageResource(R.drawable.dot_normal);
            }
            linearLayout.addView(img);
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        current = position;
    }

    @Override
    public void onPageSelected(int position) {
        //滑动设置小圆点
        for (int i = 0; i < picEntities.size(); i++) {
            ImageView img = (ImageView) linearLayout.getChildAt(i);
            img.setImageResource(R.drawable.dot_normal);
        }
        if (picEntities.size() > 0) {
            //设置圆点
            ImageView img = (ImageView) linearLayout.getChildAt(position % picEntities.size());
            img.setImageResource(R.drawable.dot_focused);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    //创建个线程
    public void backgroudThread() {
        handlerThread = new HandlerThread("handler");
        //开启线程
        handlerThread.start();
        handler = new Handler(handlerThread.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                running();
                //延迟俩秒发送一条消息
                if (flag) {
                    handler.sendEmptyMessageDelayed(10, 3000);
                }
            }
        };
    }

    //通过handler来发送一个runnable对象 设置当前页面
    public void running() {
        startHandle.post(new Runnable() {
            @Override
            public void run() {

//                index++;
                viewPager.setCurrentItem(current++);
            }
        });

    }

    //在此发送一条消息
    @Override
    public void onResume() {
        super.onResume();
        flag = true;
        handler.sendEmptyMessage(10);

    }

    //删除消息
    @Override
    public void onPause() {
        super.onPause();
        flag = false;
        handler.removeMessages(10);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        RecommendSongListFragment fragment = new RecommendSongListFragment();
        String ID = datas.getList().get(position).getListid();
        Bundle bundle = new Bundle();
        bundle.putString("ID",ID);
        fragment.setArguments(bundle);
        activity.toFramgent(fragment);


    }


    @Override
    public void onClick(View v) {
        int msg = 0;
        switch (v.getId()){
            case R.id.more:
                msg = 1;
                break;
            case R.id.moreRadio:
                msg = 2;
        }
        EventBus.getDefault().post(new MyEvent.More(msg));
    }
}
